trigger AccountTriggers on Account (before insert,before update,after update, after insert) 
{
    Map<ID,Double> accountObjectMap = new Map<ID,Double>();
    Set<ID> accountIds = new Set<ID>();
    //For Set the Account Owner to Payment User(Who Approved the records)...
    if(Trigger.isUpdate && Trigger.isBefore)
    {
        //MyAFZA proccess
        //List<Account> getMyName = [SELECT Id, Authorized_Manager_First_Name__c, Authorized_Manager_Last_Name__c FROM Account WHERE id in: Trigger.new];
        //List<Account> myAfzaPasswordList = new List<Account>();
        //End MyAFZA process
        
        for(Account accountObject : Trigger.New)
        {
            //Update the Owner to that person who approved the records...
            System.debug('In License Application Step => ' + accountObject.License_Application_Step__c);
            //if(accountObject.License_Application_Step__c == 'Approved by Front Office User' || accountObject.License_Application_Step__c == 'Approved by Immigration User' || accountObject.License_Application_Step__c == 'Approved by Archiving User')
            //{
                System.debug('In License Application Step');
                System.debug('In Record Approver Old Value => '+ Trigger.oldMap.get(accountObject.Id).Record_Approver_Id__c);
                if(Trigger.oldMap.get(accountObject.Id).Record_Approver_Id__c != accountObject.Record_Approver_Id__c)
                {
                    System.debug('In Record Approver Old Value => ');
                    if(accountObject.Record_Approver_Id__c != null || accountObject.Record_Approver_Id__c != '')
                    {
                        accountObject.ownerId = accountObject.Record_Approver_Id__c;
                        System.debug('In Record Approver Set Value ');    
                    }    
                    
                   
                }
            //}
            
           
            //For Remove Chamber History Field Population in account ...
            if(accountObject.License_Application_Type__c == 'Renewal')
            {
                if(accountObject.License_Application_Step__c == 'Approved by Immigration User')
                {
                    System.debug('In Approved by Immigration User !!!');
                
                    if(Trigger.oldMap.get(accountObject.Id).License_Application_Step__c != accountObject.License_Application_Step__c)
                    {
                        System.debug('In the Old and New Trigger !!!');
                        accountObject.Chamber_History_Number__c = null;
                    
                    }
                
                }
            
            }
            

            //MyAFZA process random password
            /*String randomStr = PasswordGenerator.getPassword(3);
            for(Account myAfza : getMyName) {
                if(accountObject.isRandomPasswordGenerated__c != true) {
                System.debug('Random Password creation fires'); 
                accountObject.MyAFZARandomPassword__c = myAfza.Authorized_Manager_First_Name__c + myAfza.Authorized_Manager_Last_Name__c + '!!!' + 'abc' + randomStr;              
                accountObject.isRandomPasswordGenerated__c = true;
                myAfzaPasswordList.add(accountObject);
                
                }
            }
            */
            //End MyAFZA process random password
                
        
        }
        
        
    }
    
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        
        //For MyAFZA process
        List<RecordType> getRecordType = [SELECT Id, Name FROM RecordType WHERE Name IN ('Under Process - Front Office Confirmed','Renewal - Front Office Confirmed') AND sObjectType = 'Account'];
        //End MyAFZA process
        
        for(Account accountObject : Trigger.New)
        {
            //For Creating the Inspection Record...    
            if(accountObject.License_Application_Type__c == 'Renewal' || accountObject.License_Application_Type__c == 'Cancellation')
            {
                System.debug('The License Application Step => '+ accountObject.License_Application_Step__c);
                if(accountObject.License_Application_Step__c == 'Submitted for Maintenance Department User Approval')
                {
                    if(Trigger.oldMap.get(accountObject.Id).License_Application_Step__c != accountObject.License_Application_Step__c)
                    {
                        if(accountObject.Number_Of_Tenancy_Contract_LocationType__c > 0)        
                        {
                            accountObjectMap.put(accountObject.Id,accountObject.Number_Of_Tenancy_Contract_LocationType__c);
                        }
                        
                    }
                    
                }   
            
            }
            else if(accountObject.License_Application_Type__c == 'Renewal Amendment')
            {
                System.debug('The License Application Step => '+ accountObject.License_Application_Step__c);
                if(accountObject.License_Application_Step__c == 'Submitted for Maintenance Department User Approval')
                {
                    if(Trigger.oldMap.get(accountObject.Id).License_Application_Step__c != accountObject.License_Application_Step__c)
                    {
                        if(accountObject.Number_Ac_Tenancy_Contract_LocationType__c > 0)        
                        {
                            accountObjectMap.put(accountObject.Id,accountObject.Number_Ac_Tenancy_Contract_LocationType__c);
                        }
                        
                    }
                    
                }   
            
            }
            
            //SLA Implementation....
            if(accountObject.License_Application_Type__c == 'Renewal' || accountObject.License_Application_Type__c == 'Renewal Amendment')
            {
                if(accountObject.License_Application_Step__c == 'Approved by Delivery Department User')
                {
                    if(Trigger.oldMap.get(accountObject.Id).License_Application_Step__c != accountObject.License_Application_Step__c)
                    {
                        accountIds.add(accountObject.Id);
                    }
                    
                }   
            
            }
            else if(accountObject.License_Application_Type__c == 'New License' || accountObject.License_Application_Type__c == 'Cancellation')
            {
                if(accountObject.License_Application_Step__c == 'Approved by BackOffice User')
                {
                    if(Trigger.oldMap.get(accountObject.Id).License_Application_Step__c != accountObject.License_Application_Step__c)
                    {
                        accountIds.add(accountObject.Id);
                    }
                    
                }   
            
            
            }
            
        }
        
        
        //For creating the Inspection Records... (For Land, Warehouse,Car Show Room) 
        if(accountObjectMap.size() > 0)
        {
            System.debug('The Account Object Map For The Inspection Record...');
            List<Tenancy_Contract__c> tenancyObjectList = new List<Tenancy_Contract__c>([select Id, Tenant__c,Name,Location__c,Location_type__c from Tenancy_Contract__c where  Tenant__c =: accountObjectMap.keyset() AND (Location_type__c = 'Land' OR Location_type__c ='Warehouse' OR Location_type__c ='Car Show Room')]);
            List<Inspection__c> inspectionInsertionList = new List<Inspection__c>();
            List<RecordType> recordType = new List<RecordType>([select id,Name,SobjectType FROM RecordType where IsActive = true AND SObjectType = 'Inspection__c' AND Name = 'License Inspection']);
            if(tenancyObjectList.size() > 0)
            {
                RecordType inspectionRecordType = [Select Id,DeveloperName,SObjectType FROM RecordType where SObjectType =: 'Inspection__c' AND IsActive =: true AND DeveloperName =: 'License_Inspection'];
                    
                for(Tenancy_Contract__c tenancyObject : tenancyObjectList)
                {
                    System.debug('In Tenancy Contract Object View  For The Inspection Record...');
                    Inspection__c inspectionObject = new Inspection__c();
                    inspectionObject.RecordTypeId = inspectionRecordType.Id;
                    inspectionObject.Company__c = tenancyObject.Tenant__c;
                    inspectionObject.Active__c = 'No';
                    inspectionObject.Location__c = tenancyObject.Location__c;   
                    inspectionObject.Immigration_Approval__c = 'Do Not Agree';
                    inspectionObject.Inspector_Approval__c = 'Do Not Agree';
                    inspectionObject.RecordTypeId = recordType[0].Id;
                    //Adding in the List...
                    inspectionInsertionList.add(inspectionObject);
                }
            }
            
            //Inserting the Inspection Records...
            if(inspectionInsertionList.size() > 0)
            {
                System.debug('In Inspection Record...');
                insert inspectionInsertionList;
            }
            
        }
        
        //For SLA Implementation.... (Record Update)
        if(accountIds.size() > 0)
        {
            List<SLA__c> slaList = new List<SLA__c>();
        
            for(SLA__c slaObject : [SELECT ID, isCompleted__c, Actual_End_Date_Time__c, Actual_Start_Date_Time__c, Amendment__c, Application_Type__c, Expected_End_Date__c, License__c,isCancelled__c, Process_Type__c, SLA_Hours__c, Status__c, Tenancy_Contract__c FROM SLA__c where License__c IN: accountIds AND isCompleted__c =: false AND Tenancy_Contract__c =: null AND Amendment__c =: null AND isCancelled__c =: false ORDER BY CreatedDate])
            {
            
                //Putting the Actual_End_Date_Time__c End Date of SLA...
                slaObject.Actual_End_Date_Time__c = DateTime.Now();
                slaObject.isCompleted__c =true;
                //Adding in the List...
                slaList.add(slaObject);
            }
            
            //Upserting the SLA List...
            if(slaList.size() > 0)
                upsert slaList;
        
        
        }
        
    }
    
    //For SLA Implementation.... (Record Creation)
    if(Trigger.isInsert && Trigger.isAfter )
    {
    
        List<SLA__c> slaList = new List<SLA__c>();
        integer slaHours = 0;
        boolean slaFlag = false;
       
        for(SLA_Custom_Setting__c slaRecord : SLA_Custom_Setting__c.getAll().values())
        {
            if(slaRecord.Application_Type__c == 'New License')
            {
                slaHours = integer.valueof(slaRecord.SLA_Hours__c);
                slaFlag  = true;
                break;
            }
                                    
        }
        
        if(slaFlag)
        {
            // Assumes the BSC work hours are the default for the Org
            List<BusinessHours> businessHoursObjectList = new List<BusinessHours>([select id from businesshours where isDefault = true LIMIT 1]) ;

            if(businessHoursObjectList.size() > 0)
            {
                for(Account accountObject : Trigger.New)
                {
                    SLA__c slaObject = new SLA__c();
                    slaObject.SLA_Hours__c = slaHours ;
                    slaObject.Actual_Start_Date_Time__c = DateTime.Now();
                   
                    // BSC works 11.5 hours / day (8:00 AM - 7:30 PM, M-F). Our SLA is 2-days (23 business hours)
                    System.debug('The Actual Start Time => '+ slaObject.Actual_Start_Date_Time__c );
                    System.debug('The Expected End Date => '+ slaObject.Actual_Start_Date_Time__c.addhours(slaHours));
                    System.debug('The Business Hour Date => ' + BusinessHours.add(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, slaHours * 60 * 60 * 1000L));
                    System.debug('The Business Hour Date GMT => ' + BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, slaHours * 60 * 60 * 1000L));
                    slaObject.Expected_End_Date__c = BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, slaHours * 60 * 60 * 1000L);   // DateTime.Now().addhours(slaHours);
                    
                    slaObject.License__c = accountObject.Id ;
                    slaObject.Process_Type__c = 'License';
                    slaObject.Application_Type__c = 'New License';
                    //Adding in the List...
                    slaList.add(slaObject);
                
                }
                
                //Now Upsert the List...
                if(slaList.size() > 0)
                    upsert slaList;

            }
        }        
    
    }
    
    
    

}